package com.hotel.controller;

import com.hotel.factory.BeanFactory;
import com.hotel.pojo.Table;
import com.hotel.service.TableService;
import com.hotel.utils.WebUtils;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by LinYaTing on 2018/11/24 23:44
 */
@WebServlet(name = "TableServlet", urlPatterns = "/table")
public class TableServlet extends BaseServlet {

    //跳转资源
    private Object uri;

    //餐桌列表展示
    public Object listTable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //调用业务逻辑
            List<Table> tableList = tableService.findAll();
            //保存
            request.setAttribute("tableList", tableList);
            uri = request.getRequestDispatcher("/sys/table/table_list.jsp");
            return uri;
    }

    //添加餐桌
    public Object addTable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
            //获取数据封装数据
            Table table = new Table();
            BeanUtils.populate(table, request.getParameterMap());
            table.setOrderTime(new Date());
            //调用业务逻辑
            tableService.save(table);
            //跳转
            uri = request.getRequestDispatcher("/table?method=listTable");
            return uri;
    }

    //删除餐桌
    public Object delete(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        try {
            //获取数据封装数据
            String id = request.getParameter("id");
            //调用业务逻辑
            tableService.delete(Integer.parseInt(id));
            //向页面传递数据只能用转发
            uri = "/table?method=listTable";
        } catch (Exception e) {
            e.printStackTrace();
            uri = "/table?method=listTable";
        }
        return uri;
    }


    //根据餐桌名查询
    public Object search(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
            //获取数据封装数据
            String tableName = request.getParameter("tableName");
            //调用业务逻辑
            List<Table> tableList = tableService.findTableByName(tableName);
            //保存
            request.setAttribute("tableList", tableList);
            //向页面传递数据只能用转发
            uri = request.getRequestDispatcher("/sys/table/table_list.jsp");
            return uri;
    }

    //退桌
    public Object returnTable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
            //获取数据封装数据
            Table table = new Table();
            BeanUtils.populate(table, request.getParameterMap());
            table.setOrderTime(new Date());
            //调用业务逻辑
            tableService.update(table);
            //向页面传递数据只能用转发
            uri = "/table?method=listTable";
            return uri;
    }


    //预定餐桌
    public Object planTable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
            //获取数据封装数据
            Table table = new Table();
            BeanUtils.populate(table, request.getParameterMap());
            table.setOrderTime(new Date());
            //调用业务逻辑
            tableService.update(table);
            //向页面传递数据只能用转发
            uri = "/table?method=listTable";
            return uri;
    }

}
